package cn.psvmc.cxzapi.util;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author ywx
 * @Classname JedisUtil
 * @Description Redis工具类
 * @Date 2019/8/2 16:48
 **/
@Component
public class JedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //redis失效时间
    @Value("${sys.redisdatatime}")
    private Integer redisdatatime;
    private ValueOperations<String, String> valueOperations;

    @PostConstruct
    public void init() {
        this.valueOperations = stringRedisTemplate.opsForValue();
    }

    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;

    public boolean set(String key, Object value, Integer expire) {
        boolean rtn = true;
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            rtn = Boolean.TRUE.equals(stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS));
        }
        return rtn;
    }

    public Boolean setObject(String key, Object value, int cacheSeconds) {
        String val = toJson(value);
        return set(key, val, cacheSeconds);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        try {
            return get(key, NOT_EXPIRE);
        } catch (Exception e) {
            return "";
        }
    }

    public void set(String key, Object value) {
        set(key, value, redisdatatime);
    }

    public void refreshToken(String key, String value) {
        Long expire = getExpire(key);
        if (expire <= 10 * 60) {
            set(key, value);
        }
    }

    //获取剩余有效期
    public Long getExpire(String key) {
        return stringRedisTemplate.getExpire(key);
    }

    public Object getObject(String key, Class<?> clazz) {
        String value = get(key);
        return fromJson(value, clazz);
    }

    public boolean delete(String key) {
        return Boolean.TRUE.equals(stringRedisTemplate.delete(key));
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }

}
